package com.spa.interfaces.admin;

import cn.dev33.satoken.annotation.SaIgnore;
import com.spa.application.command.admin.AdminRoleAddCommand;
import com.spa.application.command.admin.AdminRoleEditCommand;
import com.spa.application.dto.PageResult;
import com.spa.application.dto.admin.ResourceDTO;
import com.spa.application.dto.admin.SysRoleDto;
import com.spa.application.query.admin.AdminRoleDetailsQuery;
import com.spa.application.query.admin.AdminRoleIdQuery;
import com.spa.application.query.admin.AdminRoleQuery;
import com.spa.application.service.admin.AdminSysRoleApplicationService;
import com.spa.infrastructure.common.ResponseMessage;
import com.spa.infrastructure.mapper.SysRoleMapperStruct;
import com.spa.interfaces.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("admin/role")
public class AdminRoleController extends BaseController {

    @Resource
    private AdminSysRoleApplicationService adminSysRoleApplicationService;

    @PostMapping("add")
    @SaIgnore
    public ResponseMessage add(@Validated @RequestBody AdminRoleAddCommand command) {
        adminSysRoleApplicationService.addRole(command);
        return responseHelper.success();
    }

    @PostMapping("edit")
    @SaIgnore
    public ResponseMessage<Void> edit(@Validated @RequestBody AdminRoleEditCommand command) {
        adminSysRoleApplicationService.editRole(command);
        return responseHelper.success();
    }

    @PostMapping("listOwned")
    public ResponseMessage<List<ResourceDTO>> listOwned(@Validated @RequestBody AdminRoleIdQuery query) {
        return responseHelper.success(adminSysRoleApplicationService.listOwned(query.getRoleId()));
    }

    @PostMapping("page")
    public ResponseMessage<PageResult<SysRoleDto>> page(@Validated @RequestBody AdminRoleQuery query) {
        return responseHelper.success(adminSysRoleApplicationService.pageRole(SysRoleMapperStruct.INSTANCE.query2Condition(query)));
    }

    @PostMapping("getAll")
    public ResponseMessage<List<SysRoleDto>> all() {
        return responseHelper.success(adminSysRoleApplicationService.getAllRole());
    }

    @PostMapping("detailsById")
    public ResponseMessage<SysRoleDto> detailsById(@Validated @RequestBody AdminRoleDetailsQuery query) {
        return responseHelper.success(adminSysRoleApplicationService.detailsById(query.getRoleId()));
    }

}
